package org.dizem.groupon.action;

import com.opensymphony.xwork2.Preparable;
import org.dizem.groupon.model.domain.CityEntity;
import org.dizem.groupon.service.CityService;
import org.dizem.util.Pager;

import java.util.List;

/**
 * @author: dizem
 * @date: 11-6-25
 */
public class CityAction extends BaseAction implements Preparable {
	private CityService service;
	private List<CityEntity> cities;
	private int id;
	private CityEntity city;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public CityEntity getCity() {
		return city;
	}

	public void setCity(CityEntity city) {
		this.city = city;
	}

	public CityService getService() {
		return service;
	}

	public void setService(CityService service) {
		this.service = service;
	}

	public List<CityEntity> getCities() {
		return cities;
	}

	public void setCities(List<CityEntity> cities) {
		this.cities = cities;
	}

	public void prepare() throws Exception {
		cities = service.loadAll();
	}

	public String listCity() throws Exception {
		if (this.getRecordCount() < 0) {
			setRecordCount(service.getCount());
		}
		if (getPage() == null) {
			setPage(1);
		}
		Pager p = new Pager(getPage(), getRecordCount(), getPageSize());
		setPageCount(p.getPageCount());
		setPageUrl("listCity");
		setPage(p.getPage());
		cities = service.load(p.getStartPage(), this.getPageSize());
		return SUCCESS;
	}

	public String edit() throws Exception {
		city = service.getById(id);
		return SUCCESS;
	}

	public String update() throws Exception {
		service.update(city);
		return SUCCESS;
	}

	public String add() throws Exception {
		service.save(city);
		return SUCCESS;
	}

	public String delete() throws Exception {
		CityEntity city = service.getById(id);
		if (city != null)
			service.delete(city);
		return SUCCESS;
	}

}
